/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    struct ListNode* listA = headA, * listB = headB, * llist, * slist;
    int lenA = 1;
    int lenB = 1;
    int n = 0;
    while (listA->next)
    {
        lenA++;
        listA = listA->next;
    }
    while (listB->next)
    {
        lenB++;
        listB = listB->next;
    }
    if (listA != listB)
        return NULL;
    n = lenA - lenB;
    llist = headA;
    slist = headB;
    if (n < 0)
    {
        n = lenB - lenA;
        llist = headB;
        slist = headA;
    }
    while (n--)
    {
        llist = llist->next;
    }
    while (llist != slist)
    {
        llist = llist->next;
        slist = slist->next;
    }
    return llist;
}